package io.gitee.zhangbinhub.admin.oauth.controller.inner

import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.acp.cloud.resource.server.tools.TokenTools
import io.gitee.zhangbinhub.admin.common.api.CommonPath
import io.gitee.zhangbinhub.admin.common.api.OauthApi
import io.gitee.zhangbinhub.admin.oauth.mapper.ApplicationMapper
import io.gitee.zhangbinhub.admin.oauth.service.ApplicationService
import io.gitee.zhangbinhub.admin.resource.server.base.BaseResourceServerController
import io.gitee.zhangbinhub.admin.resource.server.vo.ApplicationVo
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.noear.solon.annotation.Controller
import org.noear.solon.annotation.Inject
import org.noear.solon.annotation.Mapping
import org.noear.solon.core.handle.Context
import org.noear.solon.core.handle.MethodType
import org.noear.solon.core.util.MimeType
import org.noear.solon.validation.annotation.Valid

@Valid
@Controller
@Api("应用信息（内部接口）")
open class InnerApplicationController(
    @Inject logAdapter: LogAdapter,
    private val applicationService: ApplicationService
) : BaseResourceServerController(logAdapter) {

    @ApiOperation("获取应用信息", notes = "根据token查询应用详细信息")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.innerBasePath + OauthApi.appInfo,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun appInfo(context: Context): ApplicationVo =
        applicationService.getApp(TokenTools.getAccessTokenModel()?.clientId ?: "")?.let {
            ApplicationMapper.INSTANCE.entityToVo(it)
        } ?: throw WebException("找不到应用信息")
}
